const API = require("../../../utils/api")
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyList: [],
    auditCompanyList: [],
    pageNum: 1,
    reachBottomShow: true,
    auditList: [],
    failList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.get_company_list()
    this.getCompanyUnderReviewList()
  },
  serviceTelEvent() {
    wx.makePhoneCall({
      phoneNumber: app.globalData.serviceTel
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.reachBottomShow) {
      let pageNum = this.data.pageNum
      pageNum++
      this.setData({
        pageNum
      })
      // console.log('分页')
      this.get_company_list()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //新增 - 企业认证流程
  add: function () {
    wx.navigateTo({
      url: '/pages/park/company/edit/edit',
    })
  },

  //路由 - 企业管理跳转
  routCompany: function (e) {
    let item = e.currentTarget.dataset.item
    let id = item.companyHomeId
    let cardId = item.cardId
    let companyName = item.companyName
    wx.navigateTo({
      url: '/pages/mine/company-manage/company-manage?id=' + id + '&cardId=' + cardId + '&companyName=' + companyName
    })
  },

  //路由 - 修改企业 
  editCompany: function (e) {
    let item = e.currentTarget.dataset.item
    let id = item.companyClaimId
    wx.navigateTo({
      url: '/pages/park/company/edit/edit?type=2&id=' + id
    })
  },

  async getCompanyUnderReviewList() {
    const res = await API.company_under_review_list()
    const auditList = res.rows.filter(item => item.sign == 0)
    const failList = res.rows.filter(item => item.sign == 2)
    this.setData({ auditList, failList })
  },

  //查询 - 当前登录人的企业 以及认证状态
  get_company_list: function () {
    let that = this
    API.get_company_supervise_list().then(res => {
      if (res.code == 200) {
        // console.log(res)
        that.setData({
          companyList: [...that.data.companyList, ...res.rows],
        })
        if (res.total <= that.data.pageNum * 10) {
          that.setData({
            reachBottomShow: false
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  }
})